const settingService = require('../service/setting.service')
const {
	ADD_CAROUSEL_FAILED,
	REMOVE_CAROUSEL_FAILED,
	UPDATE_CAROUSEL_STATUS_FAILED
} = require('../config/error')
class SettingController {
	async create(ctx, next) {
		try {
			const insertInfo = ctx.request.body
			const res = await settingService.create(insertInfo)
			ctx.body = {
				code: 0,
				message: '添加轮播图成功'
			}
		} catch (error) {
			console.log(error)
			ctx.app.emit('error', ADD_CAROUSEL_FAILED, ctx)
		}
	}
	async remove(ctx, next) {
		try {
			const { carousel_id } = ctx.params

			const res = await settingService.remove(carousel_id)
			ctx.body = {
				code: 0,
				message: '删除轮播图成功'
			}
		} catch (error) {
			console.log(error)
			ctx.app.emit('error', REMOVE_CAROUSEL_FAILED, ctx)
		}
	}
	async updateStatus(ctx, next) {
		try {
			const { carousel_id } = ctx.params
			const { status } = ctx.query
			const res = await settingService.updateStatus(carousel_id, status)
			ctx.body = {
				code: 0,
				message: '修改轮播图状态成功'
			}
		} catch (error) {
			console.log(error)
			ctx.app.emit('error', UPDATE_CAROUSEL_STATUS_FAILED, ctx)
		}
	}
	async getCarousels(ctx, next) {
		try {
			const { carouselName, status, limit, offset } = ctx.query
			const res = await settingService.getCarousels(
				carouselName,
				status,
				limit,
				offset
			)
			ctx.body = {
				code: 0,
				message: '查询成功',
				data: res
			}
		} catch (error) {
			console.log(error)
		}
	}
}

module.exports = new SettingController()
